package com.hjt.tools.generation;

import java.lang.reflect.Method;

import javax.persistence.Id;

import com.hjt.tools.generation.io.FileIO;

public class GenerationDao extends IGenerationConstant{

	private String daoTemplatePath = TEMPLATE_PATH   +"\\dao.tlb";
	
	public GenerationDao(String beanPath){
		try {
			
			Class clazz = Class.forName(beanPath);
			String bean = clazz.getSimpleName();
			String beanPage = clazz.getPackage().getName();
			String daoPage = beanPage.replace(".model", ".dao");
			templateContentList = FileIO.readFile(daoTemplatePath, flagList);
			
			flagMap.put("@#@package", daoPage);
			flagMap.put("@#@beanPath", beanPage);
			flagMap.put("@#@BEAN", bean);
			flagMap.put("@#@DAO", bean+"DAO");
			
			Method[] methods = clazz.getMethods();
			Class primaryKeyClass = String.class;
			for(int i=0,len=methods.length; i<len; i++){
				if(methods[i].isAnnotationPresent(Id.class)){
					primaryKeyClass = methods[i].getReturnType();
					break;
				}
			}
			flagMap.put("@#@ID", primaryKeyClass.getSimpleName());
			
			String daoFilePath = SRC_PATH  + "\\"+ daoPage.replace(".", "\\") + "\\" + bean+"DAO.java";
			replaceFlag();//	替换
			FileIO.writeFile(templateContentList, daoFilePath);
		} catch (ClassNotFoundException e) {
			System.out.println("对象实例化失败:" + e.getMessage());
			e.printStackTrace();
		}
		
	}
	
	
}
